package neiltwist.cfd.akka;

import static akka.actor.Actors.actorOf;
import akka.actor.ActorRef;
import akka.actor.UntypedActor;
import akka.actor.UntypedActorFactory;
import akka.dispatch.Future;

public class AkkaFactory {

	private ActorRef master;

	public AkkaFactory(final int numberOfWorkers) {
		// create the master
		master = actorOf(new UntypedActorFactory() {
			public UntypedActor create() {
				return new Master(numberOfWorkers);
			}
		}).start();
	}

	public float[][] calculateDiffuse(FloatArrayWrapper x, FloatArrayWrapper x0, float a, int N) {

		// start the calculation		
		// Should be a FloatArrayWrapper...
		Future<Object> result = master.ask(new CalculateDiffuse(x, x0, a, N));

		// check result
		result.await();
		if(result.isCompleted()) {
			scala.Option<Object> resultOption = result.result();
			if( resultOption.isDefined()) {
				Object object = resultOption.get();
				return (float[][]) object;
			} else
				return null;
		} else
			return null;

	}
}
